<template>
  <div class="list">
    <!-- 1.0 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 2.0 卡片 -->
    <el-card class="box-card">
      <!-- 3.0 表格 -->
      <!-- 
        el-table          表格分区
          data          显示的数据(数组)
        el-table-column   表格列
          prop          对应列内容的字段名(字符串)
          label         列标题
          width         宽度
          🔔 prop 写法用于展示纯文本
      -->
      <el-table :data="postList" border style="width: 100%">
        <el-table-column prop="id" label="" width="50"> </el-table-column>
        <!-- 🔔 prop 写法用于展示纯文本 -->
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column label="类型">
          <template slot-scope="scope">
            <span v-if="scope.row.type === 1">
              <i class="el-icon-tickets"></i>
              <span>文章</span>
            </span>
            <span v-else>
              <i class="el-icon-video-camera"></i>
              <span>视频</span>
            </span>
          </template>
        </el-table-column>
        <!-- 🔔 插槽写法 可以展示文本以外的内容 (图片，图标，按钮等自定义列内容) -->
        <el-table-column label="封面" width="110">
          <template slot-scope="scope">
            <!-- <i class="el-icon-time"></i> -->
            <!-- :src="scope.row.user.head_img | imgUrl" 拿到用户的图片 -->
            <img
              class="cover"
              v-if="scope.row.cover.length"
              :src="scope.row.cover[0].url | imgUrl"
            />
          </template>
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者姓名" width="180">
        </el-table-column>
        <el-table-column label="作者头像" width="110">
          <template slot-scope="scope">
            <img :src="scope.row.user.head_img | imgUrl" class="cover" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <!-- 作用域插槽 - 自定义列模板  -->
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="$router.push(`/home/post_publish/${scope.row.id}`)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card class="box-card">
      <div class="block">
        <!-- 
           @size-change      页容量改变时触发
          @current-change   页码改变时触发
          :current-page     当前页码
          :page-sizes       页容量数组(用于选择)
          :page-size        页容量
          :total            总条目数
          layout            组件布局，需要哪些功能
        -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[4, 6, 8, 10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getPostAPI } from "@/apis/news";
export default {
  data() {
    return {
      postList: [],
      // 当前选中的页码
      pageIndex: 1,
      pageSize: 4,
      total: 0,
    };
  },
  async created() {
    // 获取文章列表数据1 -  用户获取总条数的(传一个很大的页容量)
    const res = await getPostAPI({ pageSize: 1000 });
    console.log(res);
    // 获取总条数(后端的锅)
    this.total = res.data.data.length;

    // 根据初始页面和页容量第一次获取数据
    this.getPostLost();
  },
  methods: {
    // 封装数据
    async getPostLost() {
      // 获取文章列表数据2  - 用于分页展示的
      const res2 = await getPostAPI({
        pageSize: this.pageSize,
        pageIndex: this.pageIndex,
      });
      console.log(res2);
      this.postList = res2.data.data;
    },
    handleSizeChange(val) {
      console.log("页容量被改变了", val);
      this.pageSize = val;
      this.pageIndex = 1;
      this.getPostLost();
    },
    handleCurrentChange(val) {
      console.log("页码被改变了", val);
      this.pageIndex = val;
      this.getPostLost();
    },
  },
};
</script>

<style lang="less" scoped>
.box-card {
  margin-top: 20px;
}
.cover {
  width: 80px;
  height: 80px;
}
.block {
  margin-top: 20px;
  text-align: center;
}
</style>